<?php
namespace Admin\Controller;
use Think\Controller;
	/**
	 * 问题表的控制器
	 */


	class QuestionController extends CommonController {

		public function index()
    	{	

    		//问题列表的的分页
			 $count =  M('question')->where(array('shw'=>'1'))->count();
			 // var_dump($count);
			 $this->assign('count',$count);
			$Page = new \Think\Page($count,3);
			$show = $Page->show();
			$this->assign('page',$show);
    		$sql = "select t1.*,t2.nickname,t3.cname from question t1 inner join user t2 on t1.uid=t2.id inner join category t3 on t1.cid=t3.id where t1.shw='1' order by create_time desc limit  ".$Page->firstRow.",".$Page->listRows;
    		
    		$questions = M()->query($sql);

    


    		$this->assign('questions',$questions);

    		$this->display();
    	}


    	//修改展示界面
    	public function edit()
    	{	
    		// var_dump(I('get.'));
    		$id = I('get.id');
    		$cname = I('get.cname');

    		$question = M('question')->find($id);
    		// var_dump($question);
    		$this->assign('cname', $cname);
    		$this->assign('question', $question);
    		//去数据库表category的数据分配数据
	   		$cates = D('category')->getCate();

	   		$this->assign('cates', $cates);
	   		

    		$this->display();
    	}


	//编辑更新
	   public function ajaxEdit()
   {	
			
		    	$data = I('post.');
		    	// var_dump($data);exit();
		    	// $data['uid']=$_SESSION['id'];
		    	$data['create_time']=time();
		    	
		    	if (M('question')->save($data)) {
		    		
		    			$arr = array(
		    			'error'=>0,
		    			'info'=>'更新成功',
		    			// 'url'=>U('home/jie/index',array('id'=>$data['id']))
		    			);
		    		echo json_encode($arr);
		    	}else{
		    		$arr = array(
		    			'error'=>1,
		    			'info'=>'更新失败'
		    			);
		    		echo json_encode($arr);
		    		exit();
		    	}
		    	


  	}

  		public function add()
  		{
  			//去数据库表category的数据分配数据
	   		$cates = D('category')->getCate();
	   		// var_dump($cates);
	   		$this->assign('cates', $cates);
	   		

    		$this->display();
  		}

  		public function ajaxAdd()
	   {	
		  
	    	$data = I('post.');
	    	$data['uid']=$_SESSION['id'];
	    	$data['create_time']=time();
	    	// var_dump($data);exit();
	    	if (M('question')->add($data)) {
	    
	    			$arr = array(
	    			'error'=>0,
	    			'info'=>'发布成功',
	    			);
	    		echo json_encode($arr);
	    	}else{
	    		$arr = array(
	    			'error'=>1,
	    			'info'=>'发布失败'
	    			);
	    		echo json_encode($arr);
	    		exit();
	    	}
	    	
	  	}



			  	//编辑器上传图片
	   public function uploadImage()
	   {
	   		//在编辑框其中加入图片
	   		$upload = new \Think\Upload();// 实例化上传类
		    $upload->maxSize   =     3145728 ;// 设置附件上传大小
		    $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
		    $upload->rootPath  =     './Pic/'; // 设置附件上传根目录
		    $upload->savePath  =     ''; // 设置附件上传（子）目录
		    // 上传文件 
		    $info   =   $upload->upload();
		     if (!$info) {
		     	 $this->error($upload->getError());
		     }else{
		     	$path =current($info);
		     	 $path = __ROOT__."/Pic/".$path['savepath'].$path['savename'];
		     	$arr = array(
	    		'code'=>0,
	    		'msg'=>'成功',
	    		'data'=>array(
	    			'src'=>$path,
	    			'title'=>'图片',
	    			)
	    		);
	    		echo json_encode($arr);
		     }
	   }
	   //日期查询
	   public function dataCheck()
	   {
	   		$data = I('post.');
	   		$user=I('post.username');
	   		$strat = strtotime(I('post.strat'));
	   		$end =  strtotime(I('post.end'));
	   		$sql = "select t1.*,t2.nickname,t3.cname from question t1 inner join user t2 on t1.uid=t2.id inner join category t3 on t1.cid=t3.id  where t2.nickname like '%{$user}%' and t1.create_time between {$strat} and {$end}";
	   		
	   		$questions = M()->query($sql);
	   		// var_dump($questions);
	   		
	   		
	   		$this->assign('questions',$questions);
	   		$this->display();
 			  			
	   		

	   }

	   	
	   	//批量彻底删除问题
	   	public function del_All()
	   	{
	   		$id = current(I('post.'));
	   		
	   		$id = rtrim($id,',');
	   		// $sql = "update question set show='1' where id in ({$id})";
	   		$sql = "delete from question where id in ({$id})";
	   		
	   		if (M()->execute($sql)) {
		   		
		   		$arr = array(
		   				'error'=>0,
		   				'info'=>"删除成功",
		   				
		   			);
		   		 echo json_encode($arr);
		   		 exit();
	   		}

	   		
	   	}

	   	//删除一条问题(状态)
	   public function	delte()
	   {
	   	  $id=current(I('post.'));
	   	  // $sql = "delete from question where id={$id}";
	   	 $sql = "update question set shw='0' where id={$id}";

	   	  if ( M()->execute($sql)) {
		   	 
		   	  $arr = array(
		   				'error'=>0,
		   				'info'=>"删除成功"
		   			);
		   	  echo json_encode($arr);
	   	  }
	   	 
	   }
	    	//批量删除问题(定个状态)
	   	public function delAll()
	   	{
	   		$id = current(I('post.'));
	   		
	   		$id = rtrim($id,',');
	   		$arr = explode(',', $id);
	   		// var_dump($arr);exit();
	   		// $sql = "update question set show='1' where id in ({$id})";
	   		foreach ($arr as $id) {
	   			$sql = "update question set shw='0' where id={$id}";
				M()->execute($sql);
	   		}
	   		// var_dump($sql);
	   				   	$arr = array(
		   				'error'=>0,
		   				'info'=>"删除成功",
		   				'url'=>U('admin/question/index')
		   			);
		   		 echo json_encode($arr);
		   		 exit();
	   		
	   	}

	   		//删除的问题展示
	   	public function del()
    	{	

    		//问题列表的的分页
			 $count =  M('question')->where(array('shw'=>'0'))->count();
			 $this->assign('count',$count);
			$Page = new \Think\Page($count,3);
			$show = $Page->show();
			$this->assign('page',$show);
    		$sql = "select t1.*,t2.nickname,t3.cname from question t1 inner join user t2 on t1.uid=t2.id inner join category t3 on t1.cid=t3.id where t1.shw='0' order by create_time desc limit  ".$Page->firstRow.",".$Page->listRows;
    		
    		$questions = M()->query($sql);

    


    		$this->assign('questions',$questions);

    		$this->display();
    	}


	   	//彻底删除一条问题
	   public function	delteone()
	   {
	   	  $id=current(I('post.'));
	   	  $sql = "delete from question where id={$id}";
	   	 // $sql = "update question set shw='1' where id={$id}";

	   	  if ( M()->execute($sql)) {
		   	 
		   	  $arr = array(
		   				'error'=>0,
		   				'info'=>"删除成功"
		   			);
		   	  echo json_encode($arr);
	   	  }
	   	 
	   }
	   //恢复问题
	   public function recover()
	   {
	   		 $id=current(I('post.'));
	   		 $sql = "update question set shw='1' where id={$id}";
			 if ( M()->execute($sql)) {
		   	 
		   	  $arr = array(
		   				'error'=>0,
		   				'info'=>"恢复成功"
		   			);
		   	  echo json_encode($arr);
	   	  }
	   }

	}
?>